package com.meowcook.controller;

import com.meowcook.common.AppResult;
import com.meowcook.config.AppConfig;
import com.meowcook.pojo.dto.SessionDTO;
import com.meowcook.pojo.vo.SessionVO;
import com.meowcook.pojo.vo.UserPartIVO;
import com.meowcook.service.ISessionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.ArrayList;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 28792
 * Date: 2025-11-15
 * Time: 12:00
 */
@Slf4j
@Api(tags = "会话操作接口")
@RestController
@RequestMapping("/session")
public class SessionController {
    @Resource
    private ISessionService sessionService;

    @ApiOperation("创建会话")
    @PostMapping("/createSession")
    public AppResult<Long> createSession(@Valid @RequestBody SessionDTO sessionDTO) {
        log.info("会话用户id: {}", sessionDTO.getUserId());
        Long sessionId = sessionService.createSession(sessionDTO);
        return AppResult.success(sessionId);
    }

    @ApiOperation("获取会话列表")
    @GetMapping("/getSessionList")
    public AppResult<ArrayList<SessionVO>> getSessionList(HttpServletRequest  request) {
        HttpSession session = request.getSession(false);
        UserPartIVO user = (UserPartIVO) session.getAttribute(AppConfig.USER_SESSION_KEY);
        return AppResult.success(sessionService.getSessionList(user.getId()));
    }
}
